package com.xizimall.portal.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xizimall.pojo.TbItem;
import com.xizimall.pojo.TbItemDesc;
import com.xizimall.pojo.TbItemParamItem;
import com.xizimall.portal.pojo.Item;
import com.xizimall.portal.pojo.ItemInfo;
import com.xizimall.portal.service.ItemService;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.HttpClientUtil;
import com.xizimall.common.utils.JsonUtils;
/**
 * 商品信息服务
 * <p>Title: ItemServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月25日下午4:43:20
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {
    
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	
	@Value("${ITME_INFO_URL}")
	private String ITME_INFO_URL;
	
	@Value("${ITEM_DESC_URL}")
	private String ITEM_DESC_URL;
	
	@Value("${ITEM_PARAM_URL}")
	private String ITEM_PARAM_URL;
	
	@Override
	public ItemInfo getItemById(Long itemId) {
		try {
			//调用rest的服务查询商品基本信息
			String json=HttpClientUtil.doGet(REST_BASE_URL+ITME_INFO_URL+itemId);
			if(!StringUtils.isBlank(json)){
				XizimallResult xizimallResult=XizimallResult.formatToPojo(json, ItemInfo.class);
				if(xizimallResult.getStatus()==200){
					ItemInfo item=(ItemInfo) xizimallResult.getData();
					
					return item;
				}
		} 
			}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public String getItemDesc(Long itemId) {
		try {
			String json=HttpClientUtil.doGet(REST_BASE_URL+ITEM_DESC_URL+itemId);
			XizimallResult xizimallResult=XizimallResult.formatToPojo(json, TbItemDesc.class);
			if(xizimallResult.getStatus()==200){
				TbItemDesc itemDesc=(TbItemDesc) xizimallResult.getData();
				//取商品描述信息
				String result=itemDesc.getItemDesc();
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String getItemParam(Long itemId) {
		try {
			String json=HttpClientUtil.doGet(REST_BASE_URL+ITEM_PARAM_URL+itemId);
			XizimallResult xizimallResult=XizimallResult.formatToPojo(json, TbItemParamItem.class);
			if(xizimallResult.getStatus()==200){
				TbItemParamItem itemParamItem=(TbItemParamItem) xizimallResult.getData();
				String paramData=itemParamItem.getParamData();
				//生成html
				//将规格参数json数据装换成java对象
				List<Map> jsonList=JsonUtils.jsonToList(paramData, Map.class);
				StringBuffer sb=new StringBuffer();
				sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
				sb.append("    <tbody>\n");
				for(Map m1:jsonList) {
					sb.append("        <tr>\n");
					sb.append("            <th class=\"tdTitle\" colspan=\"2\">"+m1.get("group")+"</th>\n");
					sb.append("        </tr>\n");
					List<Map> list2 = (List<Map>) m1.get("params");
					for(Map m2:list2) {
						sb.append("        <tr>\n");
						sb.append("            <td class=\"tdTitle\">"+m2.get("k")+"</td>\n");
						sb.append("            <td>"+m2.get("v")+"</td>\n");
						sb.append("        </tr>\n");
					}
				}
				sb.append("    </tbody>\n");
				sb.append("</table>");
				//返回html片段
				return sb.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
